#include "../LogWorker.h"
#include "../Header/LogWorkerImpl.h"
#include "../Header/LogObjPool.hpp"
#define RETYPE(obj) ((LogWorkerImpl*)obj)

ObjPool<LogWorkerImpl> g_pool(20);

void LogWorker::setConfigPath(const std::string& conf) {
	LogSetting::getInstance().setConfigPath(conf);
    LogSetting::getInstance().init();
}

LogWorker::LogWorker(LOG_LEVEL level, const char *file, const char *fun, int line)
{
    time_t cur=time(NULL);
    hand_=g_pool.getObj();
    if(!hand_)
        hand_=new LogWorkerImpl(level,file,fun,line,cur);
    else
        RETYPE(hand_)->_setLogInfo(level,file,fun,line,cur);

}

LogWorker::~LogWorker()
{

    if(LogSetting::getInstance().logOpen())
    {

        if(RETYPE(hand_)->getLogLevel() != LOG_LEVEL::LOG_DBUG)
        {
            RETYPE(hand_)->appendData() << this->str();
            RETYPE(hand_)->pushLog();
        }
        else if(RETYPE(hand_)->getLogLevel() == LOG_LEVEL::LOG_DBUG && LogSetting::getInstance().logOpenDebug())
        {
            RETYPE(hand_)->appendData() << this->str();
            RETYPE(hand_)->pushLog();
        }
    }
    g_pool.pushObj((LogWorkerImpl*)hand_);
    hand_ = nullptr;
    
}



/*
LogWorker&& LogWorker::operator<<(const char *log)&&
{
    if (!memcmp(log, LOG_END, strlen(LOG_END)))
    {
        RETYPE(hand_)->appendData() << '\0';
        RETYPE(hand_)->pushLog();
    }
	else
        RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}

LogWorker&& LogWorker::operator<<(void* log)&&
{
    assert(log);
    char buf[9] = { 0 };
    sprintf(buf, "%p", log);
    RETYPE(hand_)->appendData()<< buf;
    return std::move(*this);
}

LogWorker&& LogWorker::operator<<(char log)&&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}

LogWorker&& LogWorker::operator<<(const std::string& log)&&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}

LogWorker&& LogWorker::operator<<(short log)&&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}

LogWorker&& LogWorker::operator<<(int log)&&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
LogWorker&& LogWorker::operator<<(unsigned short log)&&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
LogWorker&& LogWorker::operator<<(unsigned int log) &&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
LogWorker&& LogWorker::operator<<(unsigned long int log) &&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
LogWorker&&LogWorker::operator<<(unsigned long long int log) &&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
LogWorker&& LogWorker::operator<<(float  log) &&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
LogWorker&& LogWorker::operator<<(double log) &&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}

LogWorker&& LogWorker::operator<<(bool   log) &&
{
    RETYPE(hand_)->appendData()<<log;
    return std::move(*this);
}
*/
